export const promiseLimit = async function (list, fn, options = {}) {
  // 参数校验
  if (!Array.isArray(list)) {
    throw new TypeError("Expected list to be an array");
  }
  if (typeof fn !== "function") {
    throw new TypeError("Expected fn to be a function");
  }

  // 选项合并
  const { limit = 3, index = 0 } = options || {};

  // 处理空数组
  if (list.length === 0) return;

  for (let i = index; i < list.length; i += limit) {
    const batch = list.slice(i, i + limit);

    try {
      await Promise.all(
        batch.map((item, j) => {
          try {
            return fn(item, i + j);
          } catch (err) {
            // 处理同步错误
            return Promise.reject(err);
          }
        })
      );
    } catch (err) {
      // 你可以选择在这里处理错误或继续抛出
      console.error("Error in promiseLimit batch:", err);
      throw err; // 如果你希望外部能捕获错误
    }
  }
};
